package com.dwb.common.check;

import com.dwb.config.BizException;
import com.dwb.entity.*;
import com.dwb.result.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

import static com.dwb.utils.Empty.*;

/**
 * @Author manke
 * @Date 2020/6/23 17:06
 * @Version 0.0.1
 */
public class Check {
    // 校验模型数据完整性
    public static void CHECK_TECHNICIAN(SJishi technician) {

    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 9:13
     * 技师按条件查找数据校验，pageNum，pageNum不能为空
     */
    public static void CHECK_TECHNICIAN_GET_LIST(Map<String, Object> parameters) {

        if (OBJECT_EMPTY(parameters.get("pageNum"))) {
            throw new BizException(ResultCode.PAGENUM_ERROR);
        }
        if (OBJECT_EMPTY(parameters.get("pageSize"))) {
            throw new BizException(ResultCode.PAGESIZE_ERROR);
        }
    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 11:58
     * 通用获取Id不为空
     */
    public static void CHECK_GET_INFO(Map<String, String> parameters) {
        if (STRING_EMPTY(parameters.get("id"))) {
            throw new BizException(ResultCode.ID_ERROR);
        }

    }

    /**
     * @Author: wx
     * @Date: 2020/6/28 11:16
     * 通用pageNum，pageNum不能为空
     */
    public static void CHECK_GET_LIST(Map<String, String> parameters) {
        if (OBJECT_EMPTY(parameters.get("pageNum"))) {
            throw new BizException(ResultCode.PAGENUM_ERROR);
        }
        if (OBJECT_EMPTY(parameters.get("pageSize"))) {
            throw new BizException(ResultCode.PAGESIZE_ERROR);
        }
    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 9:11
     * 获取技师详情信息,判断ID不能为空
     */
    public static void CHECK_TECHNICIAN_GET_INFO(Map<String, String> parameters) {

        if (STRING_EMPTY(parameters.get("id"))) {
            throw new BizException(ResultCode.ID_ERROR);
        }
    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 9:11
     * 根据主键更新技师信息，判断technician，aptitude里的字段不能为空
     */
    public static void CHECK_TECHNICIAN_UPDATE_INFO(Map<String, Object> parameters) {
        if (OBJECT_EMPTY(parameters.get("technician"))) {
            throw new BizException(ResultCode.All_ERROR);
        }
        Object technician = parameters.get("technician");
        ObjectMapper objectMapper = new ObjectMapper();
        SJishi sJishi = objectMapper.convertValue(technician, SJishi.class);

        if (INT_EMPTY(sJishi.getId())) {
            throw new BizException(ResultCode.ID_ERROR);
        }
        if (STRING_EMPTY(sJishi.getName())) {
            throw new BizException(ResultCode.TECHNICIAN);
        }
        if (STRING_EMPTY(sJishi.getMobile())) {//电话
            throw new BizException(ResultCode.Mobile_ERROR);
        }
        if (STRING_EMPTY(sJishi.getProvince())) {//省
            throw new BizException(ResultCode.Province_ERROR);
        }
        if (STRING_EMPTY(sJishi.getCity())) {//市
            throw new BizException(ResultCode.City_ERROR);
        }
        if (STRING_EMPTY(sJishi.getAddress())) {//详细地址
            throw new BizException(ResultCode.Address_ERROR);
        }

        if (INT_EMPTY(sJishi.getBirthday())) {
            throw new BizException(ResultCode.BIRTHDAY);
        }
        if (STRING_EMPTY(sJishi.getJinengDesc())) {//技能描述
            throw new BizException(ResultCode.DESCRIBE);
        }
        if (STRING_EMPTY(sJishi.getIdcard())) {
            throw new BizException(ResultCode.Idcard_ERROR);
        }

        if (OBJECT_EMPTY(parameters.get("aptitude"))) {
            throw new BizException(ResultCode.All_ERROR);
        }
        List<Object> aptitudes = (List<Object>) parameters.get("aptitude");
        for (int i = 0; i < aptitudes.size(); i++) {
            Object o = aptitudes.get(i);
            SJishiZizhi sJishiZizhi1 = objectMapper.convertValue(o, SJishiZizhi.class);
            if (INT_EMPTY(sJishiZizhi1.getId())) {
                throw new BizException(ResultCode.ID_ERROR);
            }
            if (STRING_EMPTY(sJishiZizhi1.getImage())) {
                throw new BizException(ResultCode.Image_ERROR);
            }
            if (INT_EMPTY(sJishiZizhi1.getJishiId())) {
                throw new BizException(ResultCode.JishiId_ERROR);
            }
        }

    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 11:27
     * 服务栏目分类详情数据校验: 根据pid添加或者修改
     */
    public static void CHECK_GOODS_CATEGORY_INSET_OR_UPDATE(SGoodsCategory sGoodsCategory) {

        if (INT_EMPTY(sGoodsCategory.getPid())) {//父id
            throw new BizException(ResultCode.Pid_ERROR);
        }

        if (STRING_EMPTY(sGoodsCategory.getName())) {    // name名称
            throw new BizException(ResultCode.Name_ERROR);
        }

        if (STRING_EMPTY(sGoodsCategory.getIcon())) {  // icon图标
            throw new BizException(ResultCode.Image_ERROR);
        }

        if (STRING_EMPTY(sGoodsCategory.getDescribe())) {  // 描述
            throw new BizException(ResultCode.MiaoShu_ERROR);
        }
    }


    /**
     * @Author: wx
     * @Date: 2020/6/27 11:58
     * 服务栏目分类校验: 根据id获取分类信息详情
     */
    public static void CHECK_GOODS_CATEGORY_GET_INFO(Map<String, String> parameters) {
        if (STRING_EMPTY(parameters.get("id"))) {
            throw new BizException(ResultCode.ID_ERROR);
        }

    }


    /**
     * @Author: wx
     * @Date: 2020/6/27 14:08
     * 服务栏目分类:删除分类id校验
     */
    public static void CHECK_GOODS_CATEGORY_DELETE(Map<String, String> parameters) {
        if (STRING_EMPTY(parameters.get("id"))) {
            throw new BizException(ResultCode.ID_ERROR);
        }

    }


    /**
     * @Author: wx
     * @Date: 2020/6/27 14:33
     * 服务产品: 添加服务,修改产品校验sGoods,sGoodsPhotos,sGoodsCategoryJoins
     */
    public static void CHECK_GOODS_ADD_UPDATE(Map<String, Object> parameters) {

        if (OBJECT_EMPTY(parameters.get("sGoods")) || OBJECT_EMPTY(parameters.get("sGoodsPhotos")) || OBJECT_EMPTY(parameters.get("sGoodsCategoryJoins"))) {
            throw new BizException(ResultCode.All_ERROR);
        }
        //商品
        Object sGoods = parameters.get("sGoods");
        ObjectMapper objectMapper = new ObjectMapper();
        SGoods sGoodsV = objectMapper.convertValue(sGoods, SGoods.class);

        if (STRING_EMPTY(sGoodsV.getTitle())) {//标题
            throw new BizException(ResultCode.Title_ERROR);
        }

        if (INT_EMPTY(sGoodsV.getBrandId())) {//品牌id
            throw new BizException(ResultCode.BrandId_ERROR);
        }

        if (INT2_EMPTY(sGoodsV.getInventory())) {//库存不能为空
            throw new BizException(ResultCode.Inventory_ERROR);
        }

        if (STRING_EMPTY(sGoodsV.getSimpleDesc())) {//描述
            throw new BizException(ResultCode.MiaoShu_ERROR);
        }
        //商品相册图片
        List<Object> sGoodsPhotoss = (List<Object>) parameters.get("sGoodsPhotos");
        for (int i = 0; i < sGoodsPhotoss.size(); i++) {
            Object o = sGoodsPhotoss.get(i);
            SGoodsPhoto sGoodsPhoto = objectMapper.convertValue(o, SGoodsPhoto.class);

            if (INT_EMPTY(sGoodsPhoto.getGoodsId())) {//商品id
                throw new BizException(ResultCode.GoodsId_ERROR);
            }

            if (STRING_EMPTY(sGoodsPhoto.getImages())) {//图片
                throw new BizException(ResultCode.Image_ERROR);
            }

            if (INT_EMPTY(sGoodsPhoto.getAddTime())) {//添加时间
                throw new BizException(ResultCode.AddTime_ERROR);
            }

        }
        //商品分类关联
        Object sGoodsCategoryJoin = parameters.get("sGoodsCategoryJoins");
        SGoodsCategoryJoin sGoodsCategoryJoinV = objectMapper.convertValue(sGoodsCategoryJoin, SGoodsCategoryJoin.class);
        if (INT_EMPTY(sGoodsCategoryJoinV.getGoodsId())) {//商品id
            throw new BizException(ResultCode.GoodsId_ERROR);
        }
        if (INT_EMPTY(sGoodsCategoryJoinV.getCategoryId())) {//分类id
            throw new BizException(ResultCode.BrandId_ERROR);
        }
        if (INT_EMPTY(sGoodsCategoryJoinV.getAddTime())) {//添加时间
            throw new BizException(ResultCode.AddTime_ERROR);
        }

    }

    /**
     * @Author: wx
     * @Date: 2020/6/27 18:48
     * 获取服务产品详情信息pageNum，pageSize
     */
    public static void CHECK_GOODS_GET_LIST(Map<String, String> parameters) {
        if (OBJECT_EMPTY(parameters.get("pageNum"))) {
            throw new BizException(ResultCode.PAGENUM_ERROR);
        }
        if (OBJECT_EMPTY(parameters.get("pageSize"))) {
            throw new BizException(ResultCode.PAGESIZE_ERROR);
        }
    }

/**
* @Author: wx
* @Date: 2020/6/28 11:37
 * 新闻栏目:添加，修改判断
*/
    public static void CHECK_SNEWS_COLUM_INSET_OR_UPDATE(PNewsColumn sNewsColumn) {

        if (INT_EMPTY(sNewsColumn.getPid())) {//父id
            throw new BizException(ResultCode.Pid_ERROR);
        }

        if (STRING_EMPTY(sNewsColumn.getName())) {    // name名称
            throw new BizException(ResultCode.Name_ERROR);
        }


        if (STRING_EMPTY(sNewsColumn.getIcon())) {  // icon图标
            throw new BizException(ResultCode.Image_ERROR);
        }

        if (STRING_EMPTY(sNewsColumn.getShortName())) {    // name名称
            throw new BizException(ResultCode.ShortName_ERROR);
        }

        if (STRING_EMPTY(sNewsColumn.getDescribe())) {  // 描述说明
            throw new BizException(ResultCode.MiaoShu_ERROR);
        }
    }



}
